<?php
/*
 * Author: Quan Van Sinh
 * Email : sinhvnb@vietnambiz.com
 * Mobile: 0972405165
*/

function sendNewsletter($to,$subject, $body) {
    global $CONFIG,$db,$mail;
    $smtp=$db->query_first("SELECT * FROM smtp WHERE 1 ORDER BY RAND() LIMIT 0,1");
    $mail->IsSMTP();
    $mail->SMTPAuth = true;
    //$mail->SMTPDebug = 2;
    $mail->SetLanguage("vn", "");
    $mail->Host     = $smtp['host'];
    $mail->Username = $smtp['user'];
    $mail->Password = $smtp['pass'];
    $mail->From     =  $CONFIG["sender_email"];
    $mail->FromName = $CONFIG["name_sender_email"];
    $mail->AddAddress($to,$to);
    $mail->AddReplyTo($CONFIG["sender_email"],$CONFIG["name_sender_email"]);
    $mail->IsHTML(true);
    $mail->Subject  =  $subject;
    $mail->Body = $body;
    if(!$mail->Send()) {
        $CONFIG['error_message']=$mail->ErrorInfo;
        return false;
    } else {
         $CONFIG['success_message']="Thư đã được gửi đi thành công!";
        return true;
    }
}

function getNewsletterToSend() {
    global $db;
    return $rs = $db->query("SELECT * FROM newsletter WHERE status='yes'");
}

function listAllNews() {
    global $db,$CONFIG;
    if(intval($_REQUEST['page'])>0) {
        $page = $_REQUEST['page'];
    }else {
        $page = 1;
    }
    // neu' bam vao nut tim kiem
    if($_REQUEST['keyword']!='') {
        $searchCondition = ' AND (n.email LIKE \'%'.$_REQUEST['keyword'].'%\')';
    }

    //echo $searchCondition;
    $rsNews = $db->query("SELECT n.id FROM newsletter n WHERE 1 $searchCondition  ORDER BY n.email");
    $totalRows =  $db->num_rows($rsNews);
    $totalPages = ceil($totalRows/$CONFIG ['rows_per_page_news']);
    $start = (($page*$CONFIG ['rows_per_page_news'])-$CONFIG ['rows_per_page_news']);
    $limit = $CONFIG ['rows_per_page_news'];

    $rs = $db->query("SELECT n.id,n.email,n.status
                            FROM newsletter n 
                            WHERE 1 $searchCondition  ORDER BY n.email LIMIT $start,$limit");
    //chuyen ve` truong truoc neu' trang hien tai khong co' ban ghi nao (truong hop xoa)
    if($db->num_rows($rs)<1) {
        $page = $page - 1;
        if($page>1) {
            server_redirect(get_string_url()."&page=".$page);
        }

    }
    return array(
            "rs"=>$rs,
            "page"=>$page,
            "totalPages"=>$totalPages
    );
}

function multiActionNews() {
    global $db;
    if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'multiAction') {
        $arrNewsId = $_REQUEST['arr_news_id'];
        switch($_REQUEST['formNewsViewAction']) {
            case "delete": {
                    for($i=0;$i<count($arrNewsId);$i++) {
                        if(intval($arrNewsId[$i])>0) {
                            $db->exec_delete('newsletter','id='.intval($arrNewsId[$i]));
                        }
                    }
                    break;
            }case
            "setStatusYes": {
                    for($i=0;$i<count($arrNewsId);$i++) {
                        if(intval($arrNewsId[$i])>0) {
                            $db->exec_update('newsletter',array("status"=>'yes'),'id='.intval($arrNewsId[$i]));
                        }
                    }
                    break;
            }case
            "setStatusNo": {
                    for($i=0;$i<count($arrNewsId);$i++) {
                        if(intval($arrNewsId[$i])>0) {
                            $db->exec_update('newsletter',array("status"=>'no'),'id='.intval($arrNewsId[$i]));
                        }
                    }
                    break;
                }
        }
        header("Location: ?module=newsletter");
    }
}
?>
